angular.module("app").controller("ActivityCtrl", ["$scope", "$stateParams", "$rootScope", "$interval", "$timeout", "$http", "$state", "utils", "activity", "userInfo", function($scope, $stateParams, $rootScope, $interval, $timeout, $http, $state, utils, activity, userInfo) {
	/*页面加载完成*/
	$scope.$on('$viewContentLoaded', function(event) {

		setTimeout(function() {
			$(".container").css({
				"min-height": $(window).height(),

			})

		}, 200)
	})

	$scope.activity = activity

	/*01注册活动*/
	if($state.current.name == "01_register") {
		//		var temp = unescape(location.hash)
		//		if(temp.indexOf("uk") >= 0) {
		//			temp = temp.split("?") //分割数组
		//			var curr = temp[1].split("=") //二次分割
		//			$scope.Code = curr[1]
		//		} else {
		//			$scope.Code = ""
		//		}
		$scope.Code = $stateParams.userid
	}

	if($state.current.name == "01_register_box" || $state.current.name == "06_inviteRegister") {
		$scope.MaskShow = false //弹窗关闭显示
		$scope.seqnumber = null //短信流水号
		$scope.error_phone = false //手机号错误提示
		$scope.error_code = false //验证码错误提示
		$scope.user_code = null //user_code
		$scope.channel = $stateParams.channel //渠道

		//获取验证码
		mui(document).on("tap", "#code", function() {
			var _this = $(this)
			/*验证手机号是否注册*/
			$http.get(utils.userSys, {
				params: {
					header: utils.header("070049"),
					body: {
						"phone_number": $scope.phone
					}
				}
			}).then(function(res) {
				if(res.header.re_code == "0000") {
					/*已经注册*/
					$scope.error_phone = true

				} else if(res.header.re_code == "8000") {
					$scope.error_phone = false
					/*未注册*/
					_this.attr("disabled", "disabled")
					var count = 60
					var countTime = $interval(function() {
						count--
						if(count === 0) {
							_this.html("获取验证码")
							_this.removeAttr("disabled")
							$interval.cancel(countTime)
						} else {
							_this.html(count)
						}
					}, 1000)
					/*获取验证码*/
					$http.get(utils.userSys, {
						params: {
							header: utils.header("070052"),
							body: {
								generate_code_type: 0,
								phone_number: $scope.phone,
								user_code: ""
							}
						}
					}).then(function(res) {
						if(res.header.re_code == "0000") {
							$scope.seqnumber = res.header.request_seq
						}
					})

				}
			})

		})

		$scope.send = function() {

			/*验证手机号是否注册*/
			$http.get(utils.userSys, {
					params: {
						header: utils.header("070049"),
						body: {
							"phone_number": $scope.phone
						}
					}
				})
				.then(function(res) {
					if(res.header.re_code == "0000") {
						/*已经注册*/
						$scope.error_phone = true
						throw 0
					} else if(res.header.re_code == "8000") {
						/*未注册*/
						$scope.error_phone = false

						return $http.get(utils.userSys, {
							params: {
								header: utils.header("070053"),
								body: {
									generate_seq: $scope.seqnumber,
									verification_code: $scope.code,
									phone_number: $scope.phone
								}
							}
						})

					}
				})
				/*校验验证码*/
				.then(function(res) {
					if(res.header.re_code == "0000") {

						/*提交邀请人信息*/
						return $http.get(utils.LuckSys, {
							params: {
								header: utils.header("190070"),
								body: {
									phone_number_invitee: $scope.phone,
									user_code_inviter: $stateParams.sharecode || "" //推荐人
								}
							}
						})

					} else {
						$scope.error_code = true
						throw 0
					}

				})
				.then(function(res) {
					if(res.header.re_code == "0000") {
						return $http.get(utils.userSys, {
							params: {
								header: utils.header("070054"),
								body: {
									download: $scope.channel || 100,
									phone_id: "",
									phone_number: $scope.phone,
									register_ip: "",
									register_time: ""
								}
							}
						})
					} else {
						throw 0
					}
				})

				/*注册*/
				.then(function(res) {
					if(res.header.re_code == "0000") {
						$scope.user_code = res.body.user_code
						return $http.get(utils.userSys, {
							params: {
								header: utils.header("070055"),
								body: {
									login_type: "0",
									new_password: "",
									old_password: md5($scope.pwd),
									set_type: "0",
									user_code: $scope.user_code,
									user_type: "4",
									fingerprint_password: "",
									gesture_password: "",
									third_bind_id: "",
									third_platform_id: ""
								}
							}
						})
					} else {
						throw 0
					}
				})
				/*设置密码*/
				.then(function(res) {
					if(res.header.re_code == "0000") {
						$scope.MaskShow = true
					}
				})
				.catch(function(reason) {

				})

		}
	}

	/*02风险测评*/
	if($state.current.name == "02_Evaluation") {
		var userid = activity.getUser_code() //获取用户id
		//		$scope.tabIndex = 0 //页数索引
		$scope.evaluationTile = null //评测类型
		$scope.evaluationInfo = null //评测内容
		$scope.arr = []
		var num = 0 //分数
		//获取题目
		$http.get("json/Evaluation.json", {}).then(function(res) {
			$scope.EvaluationList = res
			$scope.arr = new Array(res.length) //创建数组
		})

		/*已经评测*/
		if(userid) {
			/*获取用户评测信息*/
			$http.post(utils.activiSys + "/risk/user_risk/user_result_list.do", {}, {
				params: {
					userId: userid
				}
			}).then(function(res) {
				if(res.list.length != 0) {
					sum(res.list[0].score)
					$scope.tabIndex = 11
				} else {
					$scope.tabIndex = 0
				}
			})
		} else {
			$scope.tabIndex = 0
		}

		//开始评测
		$scope.starEvalua = function() {
			if(!userid) {
				utils.msg("请先登录!")
				return
			}
			$http.get(utils.userSys, {
				params: {
					header: utils.header("070117"),
					body: {
						check_type: "0",
						phone_number: "",
						user_code: userid,
						phone_type: ""
					}
				}
			}).then(function(res) {
				if(res.header.re_code == "0000") {
					if(res.body.is_real_name == 0) {
						utils.msg("请先实名！")
						return
					}
					/*获取用户评测信息*/
					$http.post(utils.activiSys + "/risk/user_risk/user_result_list.do", {}, {
						params: {
							userId: userid
						}
					}).then(function(res) {
						if(res.list.length == 0) {
							$scope.tabIndex = $scope.tabIndex + 1
						} else {
							sum(res.list[0].score)
							$scope.tabIndex = 11
						}
					})

				}
			})

		}

		//确定
		$scope.Submit = function() {
			activity.web_close()
		}

		/*测评*/
		$scope.send = function() {
			if($scope.tabIndex == 10) {
				angular.forEach($scope.arr, function(value, index) {
					num += parseInt(value.num)
				})
				sum(num)
				var temps = []
				angular.forEach($scope.arr, function(value, index) {
					temps.push("{'qSerial': " + (index * 1 + 1) + ",'aSerial': " + value.serial + "}")
				})
				$http.post(utils.activiSys + "/risk/user_risk/answer.do", {}, {
					params: {
						userId: userid,
						score: num,
						result: $scope.evaluationTile,
						answers: "[" + temps.toString() + "]"
					}
				}).then(function(res) {
					if(res.resCode == "0000") {
						$scope.tabIndex = $scope.tabIndex + 1
					} else {
						utils.msg("提交失败!")
					}
				})
			}
		}
		/*点击点选框*/
		$scope.nexts = function() {
			if($scope.tabIndex < 10) {
				$timeout(function() {
					$scope.tabIndex = $scope.tabIndex + 1
				}, 100)
			}
		}
		/*重新评测*/
		$scope.resets = function() {
			$scope.tabIndex = 1
			num = 0
			$scope.arr = new Array($scope.EvaluationList.length)
		}
		/*计算总分*/
		function sum(num) {
			if(num >= 0 && num <= 60) {
				$scope.evaluationTile = "保守型"
				$scope.evaluationInfo = "您对风险损失比较敏感，投资时谨慎小心，更看重资金的安全性。"
			} else if(num > 60 && num <= 70) {
				$scope.evaluationTile = "稳健型"
				$scope.evaluationInfo = "您对风险损失较为谨慎，投资时你注重资金的安全性，追求稳健的收益回报。"
			} else if(num > 70 && num <= 80) {
				$scope.evaluationTile = "平衡型"
				$scope.evaluationInfo = "您对风险有着清晰的认识，投资时你注重在“风险最小化”和“收益最大化”之间寻找最优平衡。"
			} else if(num > 80 && num <= 90) {
				$scope.evaluationTile = "成长型"
				$scope.evaluationInfo = "您的风险承受能力较高，投资时您较为积极，乐于尝试，愿意承担一定的风险来博取较高收益。"
			} else if(num > 90 && num <= 100) {
				$scope.evaluationTile = "进取型"
				$scope.evaluationInfo = "您风险承受能力高，投资时大胆激进，勇于尝试，追求最大的投资回报，是资深的投资者。"
			}
		}

	}

	/*03测试关键字*/
	if($state.current.name == "03_Keyword") {

		if($rootScope.isapp) {
			Woption = {
				'title': '震惊！我的2018年关键词竟是这个？',
				'content': '震惊！我的2018年关键词竟是这个？',
				'icon': "http://www.beikbank.com/image/58px.png",
				'linkUrl': window.location.href,
				'code': '1',
			}
			wv.share(JSON.stringify(Woption))
		}

		var userid = activity.getUser_code() //获取用户id
		var offon = false //抽奖开关
		$scope.Prize = 0
		/*获取抽奖用户信息*/
		$http.get(utils.LuckSys, {
			params: {
				header: utils.header("190050"),
				body: {
					count: 10
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {

				$scope.cjList = res.body

			}
		})

		/*获取抽奖次数*/
		if(userid) {
			$http.get(utils.LuckSys, {
				params: {
					header: utils.header("190040"),
					body: {
						user_code: userid
					}
				}
			}).then(function(res) {
				if(res.header.re_code == "0000") {
					if(res.body.limit_count == 1) {
						offon = true
					} else {
						activity.Prize = res.body.bonus_no
						$scope.Prize = res.body.bonus_no //已经抽过奖设置奖品

						$(".FlopBox li").eq(res.body.draw_index * 1 - 1).html("<img src='image/activity/03_Keyword/0" + $scope.Prize + ".png'/>")

					}

				}
			})
		}
		/*开始抽奖*/
		$(".FlopBox li").on("click", function() {
			var index = $(this).index()
			var _this = $(this)
			if(!userid) {
				if($rootScope.isapp) {
					return utils.msg("请先登录！")
				} else {
					mui.confirm('请登录赚啦APP参与活动', ' ', ['取消', '下载'], function(e) {
						if(e.index === 1) {
							return activity.download()
						}
					}, 'div')
					return
				}

			}

			if(offon) {
				$http.get(utils.LuckSys, {
					params: {
						header: utils.header("190060"),
						body: {
							user_code: userid,
							draw_index: index + 1
						}
					}
				}).then(function(res) {
					if(res.header.re_code == "0000") {
						_this.addClass("star").siblings().unbind()
						var timeer = $timeout(function() {
							$timeout.cancel(timeer)
							activity.Prize = res.body.bonus_no //设置活动服务里奖品
							$scope.Prize = activity.Prize
							$state.go("03_Keyword_box")
						}, 2000)
					}

				})
			} else {

				utils.msg("您没有抽奖次数或您已抽过奖！")
			}

		})

	}

	/*显示关键字*/
	if($state.current.name == "03_Keyword_box") {
		$scope.Prize = activity.Prize
		if($rootScope.isapp) {
			Woption = {
				'title': '震惊！我的2018年关键词竟是这个？',
				'content': '测完还能领取最高188元红包，哦耶！',
				'icon': "http://www.beikbank.com/image/58px.png",
				'linkUrl': window.location.href,
				'code': '0',
			}
			wv.share(JSON.stringify(Woption))
		}
	}

	/*04邀请活动*/
	if($state.current.name == "04_Share") {
		var userid = activity.getUser_code() //获取用户id
		$scope.shareListShow = false
		$scope.isListshow = function() {
			if(userid) {
				$scope.shareListShow = true
			} else {
				return utils.msg("请先登录!")
			}

		}

		//阻止滚动穿透
		$(".shareList").on("touchmove", function(event) {
			event.preventDefault();
		});

		if(userid) {
			$http.get(utils.LuckSys, {
				params: {
					header: utils.header("190080"),
					body: {
						user_code: userid
					}
				}
			}).then(function(res) {
				if(res.header.re_code == "0000") {
					$scope.shareListData = res.body
				}

			})
		}

		$scope.shares = function() {
			if(!userid) {
				return utils.msg("请先登录!")
			}

			Woption = {
				'title': '我在赚啦给你送了888元红包，快来免费领取！',
				'content': '注册即送888元红包，更享新手标15%超高收益',
				'icon': "http://www.beikbank.com/image/58px.png",
				'linkUrl': window.location.origin + window.location.pathname + "#!" + "/activity/01_register?uk=SK" + userid,
				'code': '0',
			}
			wv.shareWithShareData(JSON.stringify(Woption))

		}

	}

	/*05——38祝福*/
	if($state.current.name == "05_38") {
		$scope.wdhbShow = false //我的红包
		$scope.hdgzShow = false //活动规则
		$scope.to = null //祝福对象
		$scope.froms = null //祝福人
		var userid = activity.getUser_code() //获取用户id

		/*获取祝福总数*/
		function getLuck() {
			$http.get(utils.LuckSys, {
				params: {
					header: utils.header("190120"),
					body: {

					}
				}
			}).then(function(res) {
				if(res.header.re_code == "0000") {
					$scope.wishList = res.body
				}
			})
		}
		getLuck()

		/*跳转登录*/
		function islogin() {
			if($rootScope.isapp) { //如果在APP打开直接跳登录
				wv.go(2) //跳登录
				return
			} else { //否则跳下载
				if(!userid) {
					utils.msg("请登录APP参与活动!", 1, function() {
						$scope.activity.download() //跳下载
					})
				}
				return
			}
		}

		/*查看红包*/
		$scope.lock = function() {
			if(!userid) {
				islogin()
				return
			}

			if($scope.Surplus.limit_count == 3) {
				utils.msg("请先发送祝福!")
				return
			}

			$scope.wdhbShow = true
		}

		/*送祝福*/
		$scope.giveLuck = function(index) {
			/*是否登录*/
			if(!userid) {
				islogin()
				return
			}

			/*发送祝福*/
			$http.get(utils.LuckSys, {
				params: {
					header: utils.header("190110"),
					body: {
						user_code: userid,
						wish_from: $scope.froms || $scope.Surplus.wish_from,
						wish_index: index,
						wish_to: $scope.to || $scope.Surplus.wish_to
					}
				}
			}).then(function(res) {
				if(res.header.re_code == "0000") {
					utils.msg("祝福已送上!")
					sendLuck()
					getLuck()

				}
			})

		}

		/*我的祝福*/
		if(userid) {
			sendLuck()
		}
		$scope.Surplus = {
			result: [],
			limit_count: 3
		}

		function sendLuck() {
			$http.get(utils.LuckSys, {
				params: {
					header: utils.header("190090"),
					body: {
						user_code: userid
					}
				}
			}).then(function(res) {
				if(res.header.re_code == "0000") {
					$scope.Surplus = res.body //获取剩余

					for(var i = 0; i < $scope.Surplus.result.length; i++) {
						$(".zf_list li").eq($scope.Surplus.result[i].wish_index - 1).find("button").remove()
						$(".zf_list li").eq($scope.Surplus.result[i].wish_index - 1).append('<button style="background:#b3b3b3">已送祝福</button>')
					}

				}
			})

			/*获取红包*/
			$http.get(utils.LuckSys, {
				params: {
					header: utils.header("190130"),
					body: {
						user_code: userid
					}
				}
			}).then(function(res) {
				if(res.header.re_code == "0000") {
					$scope.hongbaoList = res.body
				}

			})

		}
		//跳转
		$scope.sendHb = function() {
			if(!userid) {
				islogin()
				return
			}
			if($scope.Surplus.limit_count == 3) {
				utils.msg("请先发送祝福!")
				return
			}
			if($scope.Surplus.limit_count < 3) {
				$state.go("05_38_fx")
			}

		}

		/*获取祝福*/
		$http.get(utils.LuckSys, {
			params: {
				header: utils.header("190100"),
				body: {
					count: 10
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				$scope.luckS = res.body
				var mySwiper1 = new Swiper('.swiper-container', {
					autoplay: 1,
					speed: 4000,
					loop: true,
					freeMode: true,
					slidesPerView: 1,
					slidesPerGroup: 1,
					autoplayDisableOnInteraction: false,
					observer: true,
					observeParents: true,
				});
			}
		})

	}

	/*红包分享*/
	if($state.current.name == "05_38_fx") {
		var userid = activity.getUser_code()
		$http.get(utils.LuckSys, {
			params: {
				header: utils.header("190090"),
				body: {
					user_code: userid
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				$scope.Surplus = res.body //获取剩余
			}
		})

		/* 在APP内关闭右上角按钮*/
		if($rootScope.isapp) {
			if(mui.os.ios) {
				$scope.activity.right_close()
			} else {
				Woption = {
					'title': '3.8女神节',
					'content': '给女神送祝福还能拿TF口红领红包',
					'icon': "https://www.zhuanlalicai.com/images/58px.png",
					'linkUrl': window.location.href,
					'code': '0',
				}
				wv.share(JSON.stringify(Woption))
			}
		}

	}

	/*长期邀友07*/
	if($state.current.name == "07_InviteFriends") {
		$scope.showMask = false //二维码
		$scope.showGz = false //活动规则
		$scope.showYq = false //邀请记录
		var userid = activity.getUser_code()
		var userid2 = activity.getUser_code2()

		$scope.esc = function() {
			$scope.showMask = false
		}

		$(".ercode").on("touchmove", function(e) {
			e.preventDefault();
		})

		$scope.getYqList = function() {

			if($rootScope.isapp) {
				if(userid) {
					$http.get(utils.LuckSys, {
						params: {
							header: utils.header("190170"),
							body: {
								userCode: userid
							}
						}
					}).then(function(res) {
						if(res.header.re_code == "0000") {
							$scope.friendsData = res.body
						}
					})
					$scope.showYq = true
				} else {
					utils.msg("请先登录!")
				}
			} else {
				utils.msg("请登录APP参与活动!", 1, function() {
					$scope.activity.download() //跳下载
				})
			}

		}

		var qr = new QRCode(document.getElementById("qrcode"), {
			text: window.location.origin + window.location.pathname + "#!" + "/activity/01_register/" + userid2,
			render: "canvas",
			width: 250,
			height: 250,
			colorDark: "#000",
			colorLight: "#fff",
			correctLevel: QRCode.CorrectLevel.H
		})

		$scope.getCode = function() {
			if($rootScope.isapp) {
				if(userid) {

					$scope.showMask = true
				} else {
					utils.msg("请先登录!")
				}
			} else {
				utils.msg("请登录APP参与活动!", 1, function() {
					$scope.activity.download() //跳下载
				})
			}
		}

		$scope.getYqList = function() {

			if($rootScope.isapp) {
				if(userid) {
					$http.get(utils.LuckSys, {
						params: {
							header: utils.header("190170"),
							body: {
								userCode: userid
							}
						}
					}).then(function(res) {
						if(res.header.re_code == "0000") {
							$scope.friendsData = res.body
						}
					})
					$scope.showYq = true
				} else {
					utils.msg("请先登录!")
				}
			} else {
				utils.msg("请登录APP参与活动!", 1, function() {
					$scope.activity.download() //跳下载
				})
			}

		}

		$scope.share = function() {
			if($rootScope.isapp) {
				if(userid) {
					Woption = {
						'title': '我在赚啦给你送了888元红包，快来免费领取！',
						'content': '注册即送888元红包，更享新手标15%超高收益',
						'icon': "https://www.zhuanlalicai.com/images/58px.png",
						'linkUrl': window.location.origin + window.location.pathname + "#!" + "/activity/01_register",
						'code': '1',
					}
					wv.shareWithShareData(JSON.stringify(Woption))
				} else {
					utils.msg("请先登录!")
				}
			} else {
				utils.msg("请登录APP参与活动!", 1, function() {
					$scope.activity.download() //跳下载
				})
			}

		}

		if($rootScope.isapp) {
			$scope.activity.right_close()
		}

	}

	/*518邀友活动*/
	if($state.current.name == "08_518") {
		$scope.showMask = false //二维码
		$scope.showGz = false //活动规则
		$scope.showYq = false //邀请记录
		var userid = activity.getUser_code()
		var userid2 = activity.getUser_code2()

		$scope.esc = function() {
			$scope.showMask = false
		}

		$(".ercode").on("touchmove", function(e) {
			e.preventDefault();
		})

		/*二维码*/
		var qr = new QRCode(document.getElementById("qrcode"), {
			text: window.location.origin + window.location.pathname + "#!" + "/activity/01_register/" + userid2,
			render: "canvas",
			width: 250,
			height: 250,
			colorDark: "#000",
			colorLight: "#fff",
			correctLevel: QRCode.CorrectLevel.H
		})

		/*当面邀友*/
		$scope.getCode = function() {
			$scope.showMask = true
			if($rootScope.isapp) {
				if(userid) {
					$scope.showMask = true
				} else {
					utils.msg("请先登录!")
				}
			} else {
				utils.msg("请登录APP参与活动!", 1, function() {
					$scope.activity.download() //跳下载
				})
			}
		}

		$scope.getYqList = function() {

			if($rootScope.isapp) {
				if(userid) {
					$http.get(utils.LuckSys, {
						params: {
							header: utils.header("190170"),
							body: {
								userCode: userid
							}
						}
					}).then(function(res) {
						if(res.header.re_code == "0000") {
							$scope.friendsData = res.body
							$scope.showYq = true
						}
					})

				} else {
					utils.msg("请先登录!")
				}
			} else {
				utils.msg("请登录APP参与活动!", 1, function() {
					$scope.activity.download() //跳下载
				})
			}

		}

		/*分享邀友*/
		$scope.share = function() {
			if($rootScope.isapp) {
				if(userid) {
					Woption = {
						'title': '邀您来赚啦',
						'content': '我在赚啦给您送了888元红包和20元话费，快来领取吧！',
						'icon': "https://www.zhuanlalicai.com/images/58px.png",
						'linkUrl': window.location.origin + window.location.pathname + "#!" + "/activity/01_register",
						'code': '1',
					}
					wv.shareWithShareData(JSON.stringify(Woption))
				} else {
					utils.msg("请先登录!")
				}
			} else {
				utils.msg("请登录APP参与活动!", 1, function() {
					$scope.activity.download() //跳下载
				})
			}
		}

		if($rootScope.isapp) {
			$scope.activity.right_close()
		}

	}

	/*09答题活动*/
	if($state.current.name == "09_Answer") {

		var mySwiper = new Swiper('.swiper-container', {
			direction: 'horizontal',
			observer: true,
			observeParents: true,
		})

	}

}])